//
//  GrantlistContentViewController.m
//  FactorySale
//
//  Created by 主事丫环 on 2020/3/26.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "GrantlistContentViewController.h"
#import "GrantListContentTableViewCell.h"
#import "GrantDetailViewController.h"
#import "GrantListContentModel.h"
#import "GrantDetailViewController.h"
#import "GrantChuLiZhongViewController.h"
#import "ShenHeFinishedViewController.h"

@interface GrantlistContentViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *mydataSource;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray *listArray;

@end

@implementation GrantlistContentViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self requestData];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = Main_BackGroundColor;
    self.mydataSource = [NSMutableArray arrayWithCapacity:1];
    self.page = 1;
    [self.view addSubview:self.myTableView];
}
-(void)requestData {
    kWeakSelf;
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    NSInteger ind = [_type integerValue];
    
    ind-=1;
    NSString *stt  = [NSString stringWithFormat:@"%ld",ind];
    if (ind == -1) {
        stt = @"";
    } else if (ind == 0) {
        stt = @"1";
    } else if (ind == 1) {
        stt = @"2";
    }
    
    // 订单状态
    //空 全部
    //1.处理中
    //2.历史记录
    param[@"state"] = stt;
    param[@"pageSize"] = @"10";
    
    NSString *pagg = [NSString stringWithFormat:@"%ld",(long)_page];
    param[@"pageNo"] = pagg;
    if (_page == 1) {
          
        [weakSelf.mydataSource removeAllObjects];
        [self.listArray removeAllObjects];
       
    }
    [NetWorkTool POST:getGrantsList param:param success:^(id dic) {
        //NSLog(@"打印一下 补助金列表 列表所有数据 %@", dic);

        self.listArray = [GrantListContentModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        
        [self.mydataSource addObjectsFromArray:self.listArray];
        [self.myTableView reloadData];
        
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
    } other:^(id dic) {
        [self.myTableView reloadData];
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];

    } fail:^(NSError *error) {
        [self.myTableView reloadData];
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];

    } needUser:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _mydataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 160;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    //定义Cell标识 每一个Cell对应一个自己的标识
    NSString *CellIdentitier = [NSString stringWithFormat:@"cell%ld%ld",(long)indexPath.section, (long)indexPath.row];
    //通过唯一标识创建Cell实例
    GrantListContentTableViewCell *mycell = [tableView dequeueReusableCellWithIdentifier:CellIdentitier];
    //判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,w而不会再次初始化)
    if (!mycell) {
        mycell = [[GrantListContentTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentitier];
    }
    
    mycell.selectionStyle = UITableViewCellSelectionStyleNone;//取消cell的点击状态
    mycell.backgroundColor = [UIColor clearColor];
    GrantListContentModel *model = [_mydataSource safeObjectAtIndex:indexPath.row];

    [mycell refasf:model];
    return mycell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    GrantListContentModel *model = [_mydataSource safeObjectAtIndex:indexPath.row];
    //status状态 0待处理，1处理中 2已完成
    if ([model.status isEqualToString:@"0"]) {
        GrantDetailViewController *vc = [[GrantDetailViewController alloc] init];
        vc.idStr = model.id;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([model.status isEqualToString:@"1"]) {
        GrantChuLiZhongViewController *vc = [[GrantChuLiZhongViewController alloc] init];
        vc.idStr = model.id;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        ShenHeFinishedViewController *vc = [[ShenHeFinishedViewController alloc] init];
        vc.idStr = model.id;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(UITableView *)myTableView{
    if (!_myTableView) {
        
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT-kNaviHeight-49-kTabbarHeight) style:(UITableViewStylePlain)];
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.estimatedRowHeight = 90;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.tableFooterView = [UIView new];
        adjustInset(_myTableView);
        
        [_myTableView registerClass:[GrantListContentTableViewCell class] forCellReuseIdentifier:@"GrantListContentTableViewCell"];
        
        kWeakSelf;
        weakSelf.myTableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:_myTableView.bounds image:imgname(@"订单空界面") viewClick:^{
            [weakSelf.myTableView.mj_header beginRefreshing];
        }];
        UILabel * contentLabel;
        if (contentLabel) {
            [contentLabel removeFromSuperview];
        }
        contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-kTabbarHeight-200, ScreenW, 20)];
        contentLabel.text = @"暂无补助金申请记录";
        contentLabel.textColor = [UIColor colorWithHexString:@"#B78030"];
        contentLabel.textAlignment = YES;
        contentLabel.font = [UIFont boldSystemFontOfSize:13];
        [_myTableView.placeHolderView addSubview:contentLabel];
        self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
          
            self.page = 1;
            [self requestData];//请求数据
        }];
        self.myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.page += 1;
            [self requestData];//请求数据
        }];
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}


@end
